package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@DubboService
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }
        List<Setmeal> list = setMealDao.selectAll();
        if (!list.isEmpty()&&list.size()>0){
            log.info("从redis中删除原有数据");
            redisTemplate.delete(RedisConst.SETMEAL_ALL);
        }
        redisTemplate.opsForList().rightPushAll(RedisConst.SETMEAL_ALL,list);


    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public long countByImg(String img) {
        return setMealDao.countByImg(img);
    }

    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> list=null;
        list = redisTemplate.opsForList().range(RedisConst.SETMEAL_ALL, 0, -1);
        if (list.isEmpty()&&list.size()==0){
            //调用DAO查询所有
            list = setMealDao.selectAll();
            log.info("从数据库获取");
            redisTemplate.opsForList().rightPushAll(RedisConst.SETMEAL_ALL,list);
        }else {
            log.info("从Redis中获取");
            return list;
        }
        return list;


    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal2 = new Setmeal();
        setmeal2  =(Setmeal) redisTemplate.opsForValue().get(RedisConst.SETMEAL_BYID+id);
        if (null==setmeal2){
            //调用DAO查询数据
           setmeal2=setMealDao.selectById(id);
            redisTemplate.opsForValue().set(RedisConst.SETMEAL_BYID+id,setmeal2);
            log.info("套餐详情从数据库获取");
        }else {
            log.info("套餐详情从Redis中获取");
            return setmeal2;
        }
        return setmeal2;
    }
}
